<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2022/12/5
 * Time: 20:07
 */

namespace app\controller\sys;


use app\model\app\Apps;

use app\model\app\AppCount;
use app\model\app\AppKefuClick;
use app\model\app\AppKefuCount;
use app\model\app\AppPayOrder;
use app\model\app\AppUserBasic;
use app\model\tb\TbUser;
use app\model\view\FinishPayOrder;
use app\model\view\NotNullUserBasic;
use app\model\view\ChannelCount;

class Count extends Base{
    public function app(){
        $where = $this->where;
        $post = input('post.');
        !empty($this->start_time) && $where[] = ['time','>=',$this->start_time];
        !empty($this->end_time) && $where[] = ['time','<=',$this->end_time];
        $count = AppCount::where($where)
            ->order('time DESC')
            ->select();
        return $this->msg($count);
    }

    public function kefu(){
        $where = $this->where;
        $post = input('post.');
        !empty($this->start_time) && $where[] = ['create_time','>=',$this->start_time];
        !empty($this->end_time) && $where[] = ['create_time','<=',$this->end_time];
        $count = AppKefuCount::where($where)
            ->order('time DESC')
            ->paginate(['page' => $this->page,'list_rows' => $this->size]);
        return $this->msg($count);
    }

    public function channel(){
        $where = $map = [];
        $channel_name = input('channel_name');
        !empty($channel_name) && $map[] = $where[] = ['channel_name','like',$channel_name];
        !empty($this->start_time) && $where[] = ['create_time','>=',$this->start_time];
        !empty($this->end_time) && $where[] = ['create_time','<=',$this->end_time];
        $array = ChannelCount::where($where)->with(['channel_key'])->order('create_time DESC')->paginate(['page' => $this->page,'list_rows' => $this->size]);
        // $data = $array = [];
        // $channel = Apps::field('channel_name')->where($map)->select();
        // $start_time = strtotime($this->start_time);
        // $end_time = strtotime($this->end_time);
        // for ($i=$start_time; $i <= $end_time; $i = $i + 86400) {
        //     foreach ($channel as $key => $value) {
        //         $data[date('Y-m-d',$i)][$value['channel_name']] = [
        //             'pay_amount' => 0,
        //             'pay_count' => 0,
        //             'user_finish' => 0,
        //             'user_count' => 0
        //         ];
        //     }
        // }
        // foreach ($channel as $key => $value) {
        //     $array[$value['channel_name']] = [];
        // }
        // $array1 = FinishPayOrder::field('sum(pay_amount) AS pay_amount,count(distinct user_id) AS user_count,channel_name,create_time')->where($where)->group('channel_name,create_time')->select();
        // foreach ($array1 as $key => $value) {
        //     $array[$value['create_time']][$value['channel_name']] = [
        //         'pay_amount' => $value['pay_amount'],
        //         'pay_count' => $value['user_count']
        //     ];
        // }
        // $array2 = NotNullUserBasic::field('count(0) AS count,channel_name,create_time')->where($where)->group('channel_name,create_time')->select();
        // foreach ($array2 as $key => $value) {
        //     $array[$value['create_time']][$value['channel_name']] = [
        //         'user_finish' => $value['count']
        //     ];
        // }
        // $array3 = TbUser::field('count(0) AS count,channel_name,date(create_time) AS create_time')->where($where)->group('channel_name,date(create_time)')->select();
        // foreach ($array3 as $key => $value) {
        //     $array[$value['create_time']][$value['channel_name']] = [
        //         'user_count' => $value['count']
        //     ];
        // }
        return $this->msg($array);
    }

    public function evday()
    {
        $where = $map = $this->where;

        !empty($this->start_time) && $where[] = ['create_time', '>=', $this->start_time];
        !empty($this->end_time) && $where[] = ['create_time', '<=', $this->end_time];
        !empty($this->start_time) && $map[] = ['update_time', '>=', $this->start_time];
        !empty($this->end_time) && $map[] = ['update_time', '<=', $this->end_time];
        $data = $array = [];
        $start_time = strtotime($this->start_time);
        $end_time = strtotime($this->end_time);
        for ($i = $start_time; $i <= $end_time; $i = $i + 86400)
            $data[date('Y-m-d', $i)] = ['register_count' => 0, 'new_login_count' => 0, 'old_login_count' => 0, 'finish_order' => 0, 'finish_amount' => 0, 'finish_number' => 0, 'basic_finish_count' => 0, 'kefu_click_count' => 0, 'pay_click_count' => 0];
        $register_count = $new_login_count = TbUser::where($where)
            ->field('count(0) AS count,date(create_time) AS create_time')
            ->group('date(create_time)')
            ->select();
        foreach ($register_count as $key => $value)
            $data[$value['create_time']]['register_count'] = $value['count'];
        $old_login_count = TbUser::where($map)
            ->field('count(0) AS count,date(update_time) AS create_time')
            ->where('create_time', '<', $this->start_time)
            ->group('date(update_time)')
            ->select();
        foreach ($old_login_count as $key => $value)
            $data[$value['create_time']]['old_login_count'] = $value['count'];
        $finish_order = AppPayOrder::where($map)
            ->field('count(0) AS count,date(update_time) AS create_time')
            ->where('status', 1)
            ->group('date(update_time)')
            ->select();
        foreach ($finish_order as $key => $value)
            $data[$value['create_time']]['finish_order'] = $value['count'];
        $finish_amount = AppPayOrder::where($map)
            ->field('sum(pay_amount) AS count,date(update_time) AS create_time')
            ->where('status', 1)
            ->group('date(update_time)')
            ->select();
        foreach ($finish_amount as $key => $value)
            $data[$value['create_time']]['finish_amount'] = $value['count'];
        $finish_number = AppPayOrder::where($map)
            ->field('count(DISTINCT user_id) AS count,date(update_time) AS create_time')
            ->where('status', 1)
            ->group('date(update_time)')
            ->select();
        foreach ($finish_number as $key => $value)
            $data[$value['create_time']]['finish_number'] = $value['count'];
        $kefu_click_count = AppKefuClick::where($where)
            ->field('sum(click) AS count,date(create_time) AS create_time')
            ->group('date(create_time)')
            ->select();
        foreach ($kefu_click_count as $key => $value)
            $data[$value['create_time']]['kefu_click_count'] = $value['count'];
        $pay_click_count = AppKefuClick::where($where)
            ->field('sum(pay_click) AS count,date(create_time)  AS create_time')
            ->group('date(create_time)')
            ->select();
        foreach ($pay_click_count as $key => $value)
            $data[$value['create_time']]['pay_click_count'] = $value['count'];
        $basic_finish_count = AppUserBasic::where($map)
            ->field('count(0) AS count,date(update_time) AS create_time')
            ->where('birthday', 'NOT NULL')
            ->where('gender', 'NOT NULL')
            ->where('married', 'NOT NULL')
            ->where('fullname', 'NOT NULL')
            ->where('email', 'NOT NULL')
            ->where('employment_type', 'NOT NULL')
            ->where('uncome_monthly', 'NOT NULL')
            ->where('working_since', 'NOT NULL')
            ->where('ifsc_code', 'NOT NULL')
            ->where('branch_name', 'NOT NULL')
            ->where('bank_account_no', 'NOT NULL')
            ->where('adahaar_number', 'NOT NULL')
            ->where('adahaar_front', 'NOT NULL')
            ->where('adahaar_back', 'NOT NULL')
            ->where('score', 'NOT NULL')
            ->group('date(update_time)')
            ->select();
        foreach ($basic_finish_count as $key => $value)
            $data[$value['create_time']]['basic_finish_count'] = $value['count'];
        return $this->msg($data);

    }
    public function today(){
        $where = $this->where;
        $register_count = $new_login_count = TbUser::where($where)
            ->where('create_time','>',date('Y-m-d'))
            ->count();
        $old_login_count = TbUser::where($where)
            ->where('update_time','>',date('Y-m-d'))
            ->where('create_time','<',date('Y-m-d'))
            ->count();
        $finish_order = AppPayOrder::where($where)
            ->where('update_time','>',date('Y-m-d'))
            ->where('status',1)
            ->count();
        $finish_amount = AppPayOrder::where($where)
            ->where('update_time','>',date('Y-m-d'))
            ->where('status',1)
            ->sum('pay_amount');
        $finish_number = AppPayOrder::where($where)
            ->where('update_time','>',date('Y-m-d'))
            ->where('status',1)
            ->count('DISTINCT user_id');
        $kefu_click_count = AppKefuClick::where($where)
            ->where('create_time',date('Y-m-d'))
            ->sum('click');
        $pay_click_count = AppKefuClick::where($where)
            ->where('create_time',date('Y-m-d'))
            ->sum('pay_click');
        $basic_finish_count = AppUserBasic::where($where)
            ->where('update_time','>',date('Y-m-d'))
            ->where('birthday','NOT NULL')
            ->where('gender','NOT NULL')
            ->where('married','NOT NULL')
            ->where('fullname','NOT NULL')
            ->where('email','NOT NULL')
            ->where('employment_type','NOT NULL')
            ->where('uncome_monthly','NOT NULL')
            ->where('working_since','NOT NULL')
            ->where('ifsc_code','NOT NULL')
            ->where('branch_name','NOT NULL')
            ->where('bank_account_no','NOT NULL')
            ->where('adahaar_number','NOT NULL')
            ->where('adahaar_front','NOT NULL')
            ->where('adahaar_back','NOT NULL')
            ->where('score','NOT NULL')
            ->count();
        return json(['code' => 200,'msg' => '','data' => [
            'register_count' => $register_count,
            'new_login_count' => $new_login_count,
            'old_login_count' => $old_login_count,
            'finish_order' => $finish_order,
            'finish_amount' => $finish_amount,
            'finish_number' => $finish_number,
            'basic_finish_count' => $basic_finish_count,
            'kefu_click_count' => $kefu_click_count,
            'pay_click_count' => $pay_click_count
        ]]);
    }
    public function total(){
        $where = $this->where;
        $finish_order = AppPayOrder::where($where)->sum('pay_amount');
        $finish_amount = AppPayOrder::where($where)->where('status',1)->sum('pay_amount');
        return json(['code' => 200,'msg' => '','data' => [
            'finish_order' => $finish_order,
            'finish_amount' => $finish_amount
        ]]);
    }
}